package com.cloudlinkscm.base.authcenter.service.impl;

import com.cloudlinkscm.base.authcenter.constant.ResourceTypeEnum;
import com.cloudlinkscm.base.authcenter.dao.PublicResourceDao;
import com.cloudlinkscm.base.authcenter.domain.AbstractPublicResourceManager;
import com.cloudlinkscm.base.authcenter.exception.PublicResourceKeyRepeatException;
import com.cloudlinkscm.base.authcenter.exception.PublicResourceParentNotFindException;
import com.cloudlinkscm.base.authcenter.exception.PublicResourceTypeIncorrectException;
import com.cloudlinkscm.base.authcenter.model.PublicResource;
import com.cloudlinkscm.base.authcenter.model.dto.*;
import com.cloudlinkscm.base.authcenter.service.PublicResourceService;
import com.cloudlinkscm.base.authcenter.model.dto.ResourceTree;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by sin on 2017/8/7.
 */
@Service
public class PublicResourceServiceImpl implements PublicResourceService {


	@Autowired
	private PublicResourceDao publicResourceDao;
	@Autowired
	private AbstractPublicResourceManager abstractPublicResourceManager;

	/**
	 * 公共资源：保存
	 *
	 * @param saveDTO 需要保存的信息
	 * @return 返回影响的条数
	 */
	@Override
	public PublicResource save(PublicResourceSaveDTO saveDTO) {

		// pid 等于 null 设置默认值为 root
		if (saveDTO.getPid() == null) {
			saveDTO.setPid(ResourceTree.TREE_ROOT);
		}

		if (!ResourceTree.TREE_ROOT.equals(saveDTO.getPid())) {
			if (publicResourceDao.selectByPrimaryKey(saveDTO.getPid()) == null) {
				throw new PublicResourceParentNotFindException();
			}
		}

		if (publicResourceDao.selectByResourceKey(saveDTO.getResourceKey()) != null) {
			throw new PublicResourceKeyRepeatException();
		}

		if (ResourceTypeEnum.MENU.code == saveDTO.getResourceType()) {
			SubPublicResourceCreateDto createDto = new SubPublicResourceCreateDto();
			BeanUtils.copyProperties(saveDTO, createDto);

			return abstractPublicResourceManager.createMenu(createDto);
		}
		else if (ResourceTypeEnum.ACTION.code == saveDTO.getResourceType()) {
			PermissionPublicResourceCreateDto createDto = new PermissionPublicResourceCreateDto();
			BeanUtils.copyProperties(saveDTO, createDto);

			return abstractPublicResourceManager.createAction(createDto);
		}
		else if (ResourceTypeEnum.MODULE.code == saveDTO.getResourceType()) {
			PublicResourceCreateDto createDto = new PublicResourceCreateDto();
			BeanUtils.copyProperties(saveDTO, createDto);

			return abstractPublicResourceManager.createModule(createDto);
		}
		else if (ResourceTypeEnum.PAGE.code == saveDTO.getResourceType()) {
			PermissionPublicResourceCreateDto createDto = new PermissionPublicResourceCreateDto();
			BeanUtils.copyProperties(saveDTO, createDto);

			return abstractPublicResourceManager.createPage(createDto);
		}

		throw new PublicResourceTypeIncorrectException();
	}


	/**
	 * 公共资源：更新
	 *
	 * @param updateDTO 需要更新的数据
	 * @return 返回影响的条数
	 */
	@Override
	public PublicResource update(PublicResourceUpdateDTO updateDTO) {
		return abstractPublicResourceManager.update(updateDTO);
	}


	/**
	 * 公共资源：删除
	 *
	 * @param id 删除的 id
	 * @return 返回影响的条数
	 */
	@Override
	public int remove(String id) {
		return abstractPublicResourceManager.delete(id);
	}



	/**
	 * 公共资源：tree
	 *
	 * <p>
	 *     一次性返回所有节点
	 * </p>
	 *
	 * @return 返回全部的 tree 节点
	 */
	@Override
	public List<ResourceTree> tree() {
		return abstractPublicResourceManager.tree();
	}
}
